import java.util.Arrays;

public class Main {
    //改变原有数组元素的值
    public static void tansform(int [] arr) {
        for(int i = 0; i < arr.length; i++) {
            System.out.println(2 * arr[i]);
        }
    }

    public static void main1(String[] args) {
        int [] arr = {1,2,3};
        tansform(arr);
    }

    //奇数位于偶数之前
    public static void sort(int [] arr) {
        int left = 0;
        int right = arr.length - 1;
        while(left < right) {
            //左指针找到偶数时停止
            while(left < right && arr[left] % 2 !=0) {
                left ++;
            }
            //右指针找到奇数时停止
            while(left < right && arr[right] % 2 == 0) {
                right --;
            }
            //交换位置
            if(left < right) {
                int tmp = arr[left];
                arr[left] = arr[right];
                arr[right] = tmp;
            }
        }
    }

    public static void main2(String[] args) {
        int [] arr = {1,2,3,4,5,6};
        sort(arr);
        for(int x :arr) {
            System.out.println(x);
        }
    }

    //两数之和
    public static void sum(int[] arr, int target) {
        for(int i = 0; i < arr.length; i++) {
            for(int j = i + 1; j < arr.length; j++) {
                if(arr[i] + arr[j] == target){
                    System.out.println("[" + i + "," + j + "]");
                    System.out.println("因为" + arr[i] + "+" + arr[j] + "==" + target);
                }
            }
        }
    }

    public static void main3(String[] args) {
        int [] num = {2,7,11,15};
        int targrt = 9;
        sum(num,targrt);
    }

    //只出现一次的数字
    public static void Look(int [] arr){
        for(int i = 0; i < arr.length; i++) {
            boolean flag = true;
            for(int j = i + 1; j <arr.length; j++) {
                if(i != j && arr[i] == arr[j]) {
                    flag =false;
                    break;
                }
            }
            if(flag == true){
                System.out.println("[" + arr[i] + "]");
                return;
            }
        }
    }

    public static void main4(String[] args) {
        int[] arr = {4,1,2,1,2};
        Look(arr);
    }

    //多数元素
    public static void much(int [] arr) {
        for(int i= 0; i < arr.length; i++) {
            int count = 0;
            for(int j = 0; j < arr.length; j++) {
                if(arr[i] == arr[j]) {
                    count++;
                }
            }
            if(arr.length / 2 < count) {
                System.out.println(arr[i]);
                return;
            }
        }
    }

    public static void main5(String[] args) {
        int[] arr = {2,2,1,1,1,2,2};
        much(arr);
    }

    //存在连续三个奇数的数组
    public static boolean isJiShu(int[] arr){
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] % 2 != 0 &&arr[i + 1] % 2 != 0&&arr[i + 2] % 2 != 0) {
                return true;
            }
        }
        return false;
    }

    public static void main6(String[] args) {
        int[] arr = {1,2,34,3,4,5,6,23,12};
        boolean ret1 = isJiShu(arr);
        System.out.println(ret1);
    }

    //冒泡排序
    public static int[] Bubble(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
        return arr;
    }

    public static void main7(String[] args) {
        int[] arr = {1,5,4,3,24,6,7};
        Bubble(arr);
        System.out.println(Arrays.toString(arr));
    }

    //二分查找
    public static int ret(int[] arr, int target){
        int left = 0;
        int right = arr.length - 1;
        while(left <= right) {
            int mid = ((right - left) + left) / 2;
            if(arr[mid] > target) {
                right = mid - 1;
            }else if (arr[mid] < target){
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        int target = 2;
        int index = ret(arr,target);
        System.out.println(index);
    }
}